<!--
   Copyright 2024 The Google Research Authors.
  
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
  
       http://www.apache.org/licenses/LICENSE-2.0
  
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
-->

<!DOCTYPE html>
<html>
<head>
<title>Search Engine Task</title>
<!-- stylesheets -->
<link rel="stylesheet" type="text/css" href="../core/core.css">
<link rel="stylesheet" type="text/css" href="../core/jquery-ui/jquery-ui.min.css">
<style>
  #area { height: 156px; position: relative; }
  #area input { width: 80px !important; }
  #search-bar { margin: 2px; }
  #page-content { margin: 2px; }
  #page-content a { color: #0000EE; font-size: 10px; font-weight: bold; text-decoration: underline; }
  #page-content a:visited { color: #0000EE; }
  .search-url { color: #006621; }
  #pagination { font-size: 15px; margin: 0; position: absolute; bottom: 2px; }

  /* styling for the pagination widget */
  .pagination > li { display: inline; margin: 0 2px; }
  .pagination a:visited { color: #0000EE !important; }
  .disabled { display: none !important; }
  .page-item.active a { color: #000000; text-decoration: none; }
  .page-item.first, .page-item.last { display: none !important; }

  .alink { text-decoration: underline; color: blue; cursor: pointer; }
  #dialog { height: auto !important; }
  .ui-dialog { font-size: 10px; }
  .ui-dialog .ui-dialog-titlebar { padding: 2px 3px; height: 15px; }
  .ui-button { outline: 0; }
  .ui-dialog .ui-dialog-content { padding: 0px; margin: 0 5px; }
  /* 160 + 80 + 50 + 10 */
  #wrap,
  #sync-task-cover {
    height: 300px;
    width: 160px;
  }

  #click-canvas,
  #reward-display {
    left: 165px;
  }

  #query {
    background-color: #FF0;
    font-size: 10px;
    height: 54px;
    padding: 3px;
  }
</style>

<!-- JS -->
<script src="../core/core.js"></script>
<script src="../core/d3.v3.min.js"></script>
<script src="../core/jquery-ui/external/jquery/jquery.js"></script>
<script src="../core/jquery-ui/jquery-ui.min.js"></script>
<script src="../common/ui_utils.js"></script>
<script src="../common/special/search-engine/jquery.twbsPagination.min.js"></script>

<script>
core.EPISODE_MAX_TIME = 2000000;  // set episode interval to 2000 seconds

// click-dialog
var resetUIDialog = function(div){
  if(div.html().length > 0) $('#dialog').dialog('destroy');
  div.empty(); // clear previous problem, if any
}

var createDialog = function(div){
  var html = '<p>' + ui_utils.generateWords(4,8) + '</p>';
  div.append(html);

  $('#dialog').dialog({ height: 70, position: {my: 'center', at: 'center', of: document.getElementById('area')} });

  $('.ui-dialog')[0].style.margin = core.randi(-10,20) + 'px ' + core.randi(5,25) + 'px';
  $('.ui-dialog')[0].style.width = core.randi(90,120) + 'px';
  $('.ui-dialog')[0].style.height = core.randi(70,100) + 'px';
}

// search-engine
var DIV_TEMPLATE =
  `<a href='#' class='search-title'></a>
  <div class='search-url'></div>
  <div class='search-desc'></div>`
var TOTAL_RESULTS = 9;
var POSITION = ['', 'st', 'nd', 'rd'];
var DOMAINS = ['com', 'net', 'org', 'ca', 'us', 'gov', 'pizza', 'gg', 'tv', 'co.uk', 'it', 'eu', 'hk', 'mx', 'se', 'jp', 'io', 'rocks'];

var createSearchElem = function(result, index){
  var div = document.createElement('div');
  div.innerHTML = DIV_TEMPLATE;
  div.getElementsByClassName('search-title')[0].innerHTML = result.title;
  div.getElementsByClassName('search-title')[0].setAttribute('data-result', index);
  div.getElementsByClassName('search-url')[0].innerHTML = result.url;
  div.getElementsByClassName('search-desc')[0].innerHTML = result.desc;
  $('#page-content').append(div);
}

var generateSearch = function(){
  var results = [];
  var names = core.shuffle(ui_utils.FIFTY_NAMES.slice());
  for(var i=0;i<TOTAL_RESULTS;i++){
    var result = {}
    var subdomain = core.sample(ui_utils.lorem_words).replace(',', '');
    var domain = core.sample(ui_utils.lorem_words).replace(',', '');
    result.title = core.sample(names);
    result.url = 'https://' + core.sample(['www.', '']) + core.sample(['', subdomain]) + domain +  '.' + core.sample(DOMAINS);
    result.desc = ui_utils.generateWords(core.randi(2,4));
    results.push(result);
  }
  return results;
}

var resetUI = function(){
  $('#search-text').val('');
  $('#search').unbind();
  $('#page-content').empty();
  $('#pagination').empty();
}

var searchEngine = function(){
  var results = generateSearch();
  var expectedSearch = core.sample(core.shuffle(ui_utils.FIFTY_NAMES.slice()));
  var expectedIndex = core.randi(0,9);
  results[expectedIndex].title = expectedSearch;

  var pos = (expectedIndex+1) + 'th';
  if(expectedIndex < 3) pos = (expectedIndex+1) + POSITION[expectedIndex+1];
  return { results: results, expectedSearch: expectedSearch, expectedIndex: expectedIndex, pos: pos };
}

var bindClickEvents = function(problemSet){
  
}

var genProblem = function() {
  var task_state = -1.0;

  resetUI();
  var problemSet = searchEngine();
  var dialogDiv = $('#dialog');
  resetUIDialog(dialogDiv);
  createDialog(dialogDiv);

  $('button.ui-button').on('click', function(){ task_state=1.0; console.log('click-dialog', task_state);})


  // $('#query').html('Close the dialog box by clicking the "x", and then use the textbox to enter "<span class="bold">' + problemSet.expectedSearch + '</span>" and press "Search", then find and click the <span class="bold">' + problemSet.pos + '</span> search result.');
  $('#query').html('Use the textbox to enter "<span class="bold">' + problemSet.expectedSearch + '</span>" and press "Search", then find and click the <span class="bold">' + problemSet.pos + '</span> search result, after closing the dialog box by clicking the "x"');

  $('#search').on('click', function(){
    // clear the existing pagination modal, and rebuild it on click
    $('#pagination').twbsPagination('destroy');

    $('#pagination').twbsPagination({
      totalPages: 3,
      visiblePages: 3,

      onPageClick: function (event, page) {
        $('#page-content').empty();
        var userSearch = $('#search-text').val();

        // generate fake results when the user's search doesn't match.
        if(userSearch.toLowerCase() !== problemSet.expectedSearch.toLowerCase()){
          var fakeResults = generateSearch().slice(0, 3);
          for(var p=0;p<3;p++) createSearchElem(fakeResults[p], -1);
        } else {
          $('#page-content').empty();
          var startIndex = (page-1)*3;
          var currentResults = problemSet.results.slice(startIndex, startIndex+3);
          for(var p=0;p<3;p++) createSearchElem(currentResults[p], startIndex+p);
        }

        $('#page-content a').unbind();
        $('#page-content a').on('click', function(){
          if(userSearch.toLowerCase() !== problemSet.expectedSearch.toLowerCase()) core.endEpisode(-1.0);
          else {
            var linkIndex = this.getAttribute('data-result');
            var r = linkIndex === problemSet.expectedIndex.toString() ? 1.0 : -1.0;
            console.log('search-engine', r)
            if (task_state == 1.0) {core.endEpisode(r, r > 0);} else {core.endEpisode(-1.0);}
          }
        });

      },
      prev: '<',
      next: '>'
    });
  });
}

window.onload = function() {
  core.startEpisode();
}
</script>
</head>
<!-- Base HTML -->
<body>
<div id="wrap">
  <div id="query"></div>
  <div id="area">
    <div id="dialog"></div>
    <div id="search-bar">
      <input type="text" id="search-text">
      <button id="search">Search</button>
    </div>
    <div id="page-content"></div>
    <ul id="pagination"></ul>
  </div>
</div>
</body>
</html>
